package com.aigou.admin.dao.impl;

import com.aigou.admin.utils.JDBCUtils;
import com.aigou.admin.dao.SearchHistoryDao;
import com.aigou.admin.entity.SearchHistory;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * @author chai
 * @date 2024/12/8 0008
 * @Description
 */

@Repository
public class SearchHistoryDaoIpml implements SearchHistoryDao {
    @Override
    public List<SearchHistory> selectAll() {
        return JDBCUtils.executeQuery(SearchHistory.class, "select * from s_search_history");
    }

    @Override
    public SearchHistory selectById(String id) {
        List<SearchHistory> searchHistory= JDBCUtils.executeQuery(SearchHistory.class, "select * from s_search_history where id = ? ",  id);
        if (searchHistory.isEmpty()) {
            return null;
        }
        return searchHistory.get(0);
    }

    @Override
    public Boolean insert(SearchHistory searchHistory) {
        int rows = JDBCUtils.executeUpdate("insert into s_search_history(id,search_words,num,search_time) values(?,?,?,?)"
                , searchHistory.getId(),searchHistory.getSearch_words(),searchHistory.getNum(),searchHistory.getSearch_time());
        return rows > 0;
    }

    @Override
    public Boolean update(SearchHistory searchHistory) {
        int rows = JDBCUtils.executeUpdate("update s_search_history set num=?,search_time=?,search_words=? where id = ?"
                ,searchHistory.getNum(),searchHistory.getSearch_time(),searchHistory.getSearch_words(),searchHistory.getId());
        return rows > 0;
    }

    @Override
    public Boolean delete(String id) {
        int rows = JDBCUtils.executeUpdate("delete from s_search_history where id = ?", id);
        return rows >0;
    }

    @Override
    public Integer selectOrderCount() {
        return JDBCUtils.executeCount("select count(*) from s_search_history");
    }

    @Override
    public List<SearchHistory> selectSearchHistoryByLimit(Integer start, Integer rows) {
        return JDBCUtils.executeQuery(SearchHistory.class,"select * from s_search_history limit ?,?",start,rows);
    }

    @Override
    public Integer selectOrderCountByLike(String k) {
        return JDBCUtils.executeCount("select count(*) from s_search_history  where search_words like ? ",'%'+k+'%');
    }

    @Override
    public List<SearchHistory> selectSearchHistoryByLike(Integer start, Integer rows, String k) {
        return JDBCUtils.executeQuery(SearchHistory.class,"select * from s_search_history where search_words like ? limit ?,?",'%'+k+'%',start,rows);
    }


}
